// Description: 描述
package tools

import "math/rand"

const (
	AlphaStr                   = iota // 字母字
	AlphaLowerStr                     // 小写字母
	AlphaUpperStr                     // 大写字母
	NumericStr                        // 数字
	AlphaNumericStr                   // 数字字母
	NoZeroNumericStr                  // 无0数字
	AlphaLowerNoZeroNumericStr        // 小写字母和无0数字
	NumericAlphaStr                   // 数字和大小写字母
)

var (
	AlphaRunes                   = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	AlphaLowerRunes              = []rune("abcdefghijklmnopqrstuvwxyz")
	AlphaUpperRunes              = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	AlphaNumericRunes            = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	NumericRunes                 = []rune("0123456789")
	NoZeroNumericRunes           = []rune("123456789")
	AlphaLowerNoZeroNumericRunes = []rune("123456789abcdefghijklmnopqrstuvwxyz")
	NumericAlphaRunes            = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
)

// GenStr 生成指定长度的字符串
func GenStr(mode, length int) string {
	var seedStr []rune

	switch mode {
	case AlphaStr:
		seedStr = AlphaRunes
	case AlphaLowerStr:
		seedStr = AlphaLowerRunes
	case AlphaUpperStr:
		seedStr = AlphaUpperRunes
	case NumericStr:
		seedStr = NumericRunes
	case AlphaNumericStr:
		seedStr = AlphaNumericRunes
	case NoZeroNumericStr:
		seedStr = NoZeroNumericRunes
	case AlphaLowerNoZeroNumericStr:
		seedStr = AlphaLowerNoZeroNumericRunes
	case NumericAlphaStr:
		seedStr = NumericAlphaRunes
	default:
		return ""
	}

	b := make([]rune, length)
	seedLen := len(seedStr)
	for i := 0; i < length; i++ {
		b[i] = seedStr[rand.Intn(seedLen)]
	}

	return string(b)
}

// GenNumeric 生成指定范围的数字
func GenNumeric(min int, max int) int {
	if min < max {
		return rand.Intn(max-min) + min
	} else {
		return rand.Intn(min-max) + max
	}
}
